const enterpriseVue = new Vue({
    el: '#app',
    data() {
        return {
            rules: {

            },
            form: [{
                Group_Rotation_SerialNumber: '',
                Group_Rotation_Name: '',
                Group_Rotation_URL: '',
                Picture_URL: '',
            }],
            loading: false,
            dialogImageUrl: '',
            dialogVisible: false,
            fileList: [],
            index:0,
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        add_form() {
            this.form.push({
                Group_Rotation_SerialNumber: '',
                Group_Rotation_Name: '',
                Group_Rotation_URL: '',
                Picture_URL: '',
            })
        },
        remove_form() {
            if (this.form.filter(item => item.Row_Status != '1').length > 0) {
                let i = this.form.filter(item => item.Row_Status != '1').length - 1
                let obj = this.form.filter(item => item.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    this.form.pop();
                } else {
                    this.$confirm('警告！此操作将删除一条已有的轮播图, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        this.form.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        getData(){
            const _this = this;
            $.ajax({
              url: DomainName + '/GoodsFileInquiry_Original',
              type: 'POST',
              data:{
                Data:'group_rotation_pic',
              },
              success: function (res) {
                _this.form = res
                console.log(res);
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
        },
        submitForm(formName) {
            const _this = this
            if (!this.loading) {
                const { add, modify } = _this.deal()
                let data = {}
                if (add == null) {
                    data = { Data: JSON.stringify({ Modify: { group_rotation_pic: modify } })}
                } else if (modify == null){
                    data = { Data: JSON.stringify({ Add: { group_rotation_pic: add } })}
                } else {
                    data = { Data: JSON.stringify({ Modify: { group_rotation_pic: modify } , Add: { group_rotation_pic: add } })}
                }
                console.log(JSON.parse(data.Data));
                _this.loading = true
                $.ajax({
                    url: `${DomainName}/getEditAction?token=${$.cookie("token")}`,
                    type: 'POST',
                    data,
                    success: function (res) {
                        if (res.Status === 'success') {
                            _this.$message.success("提交成功")
                            window.history.go(-1);
                        } else {
                            _this.$message.error("提交失败，请稍后再试")
                        }
                    },
                    error: function () {
                        _this.$message.error("操作失败，请稍后再试")
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                        }
                    }
                })
            }
        },
        upload(file) {
            const _this = this
            new Compressor(file, {
                quality: 1,
                success(file) {
                    const formData = new FormData();
                    formData.append('file', file, file.name);
                    let oFileReader = new FileReader();
                    oFileReader.readAsDataURL(file);
                    oFileReader.onload = function (e) {
                        $.ajax({
                            type: 'POST',
                            url: DomainName + '/FileList',
                            data: {
                                Data: e.target.result,
                                DataLength: file.size,
                                FileName: file.name
                            },
                            complete: function (XMLHttpRequest) {
                                _this.loading = false
                                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                    window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                                }
                            },
                            success: function (Result) {
                                if (Result.Status == "success") {
                                    _this.form.filter(item => item.Row_Status != '1')[_this.index].Picture_URL = Result.Picture_Url
                                    _this.$message.success("上传成功")
                                } else {
                                    _this.$message.error(Result.msg)
                                }
                            },
                            error: function () {
                                _this.$message.error("操作失败，请稍后再试")
                            },
                            dataType: "json"
                        });
                    }
                },
                error(err) {
                    console.log(err.message);
                },
            });
        },
        deal() {
            let add = [], modify = []
            for (const i in this.form) {
                const obj = this.form[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add.push(obj)
                } else {
                    modify.push(obj)
                }
            }
            if (add.length == 0) {
                return { add: null, modify };
            } else if (modify.length == 0){
                return { add, modify:null };
            }
            return { add, modify };
        },
        handleIndex(index){
            this.index = index
        },
        //查看图片
        handlePictureCardPreview(file) {
            this.dialogVisible = true
            this.dialogImageUrl = file
        },
        //删除图片
        handleRemove(index) {
            this.form.filter(item => item.Row_Status != '1')[index].Picture_URL = ''
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})